﻿using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
    /// <summary>
    /// 难度速度集合
    /// </summary>
    private float[] speed =
    {
        20.0f,//简单
        40.0f,//中等
        80.0f,//困难
    };
    
    //动画组件
    private Animator m_Anim;

    //动画现在状态
    private AnimatorStateInfo m_CurrentBaseState;
    
    /// <summary>
    /// startPosFlag
    /// </summary>
    private bool startPosFlag;
    /// <summary>
    /// 手指开始位置
    /// </summary>
    private Vector2 startFingerPos;
    /// <summary>
    /// 手指当前位置
    /// </summary>
    private Vector2 nowFingerPos;
    /// <summary>
    /// 当前速度
    /// </summary>
    private float currentSpeed;
    /// <summary>
    /// 开始
    /// </summary>
    private void Start()
    {
        currentSpeed = speed[BattleLogic.getInstance().getDiffculty()];
        //获得主角身上的Animator组件
        m_Anim = GetComponent<Animator>();
        startPosFlag = true;
    }

    /// <summary>
    /// 奔跑
    /// </summary>
    public void run()
    {
        currentSpeed = speed[BattleLogic.getInstance().getDiffculty()];
        m_Anim.SetBool("idle", false);
        m_Anim.SetBool("run", true);
    }
    
    /// <summary>
    /// 待机
    /// </summary>
    public void idle()
    {
        currentSpeed = 0;
        m_Anim.SetBool("idle", true);
        m_Anim.SetBool("run", false);
    }
    
    /// <summary>
    /// 更新
    /// </summary>
    private void Update()
    {
        if (BattleLogic.getInstance().GetIsPause())
        {
            if (!m_Anim.GetBool("idle"))
            {
                idle();
            }
        }
        else
        {
            if (!m_Anim.GetBool("run"))
            {
                run();
            }
        }


        ///向前跑
        if (BattleLogic.getInstance().GetIsPause())
        {
            transform.parent.position += Vector3.right * currentSpeed * Time.deltaTime;
        }
        else
        {
            currentSpeed = speed[BattleLogic.getInstance().getDiffculty()];
            transform.parent.position += Vector3.right * currentSpeed * Time.deltaTime;
        }

        m_Anim.speed = BattleLogic.getInstance().getDiffculty() + 1;

#if PLATFORM_STANDALONE_WIN || UNITY_EDITOR_WIN
    //获得现在播放的动画状态
        m_CurrentBaseState = m_Anim.GetCurrentAnimatorStateInfo(0);
        if (Input.GetKeyDown(KeyCode.A))
        {
            // idle();
            ChangePlayerZ(true); //移动主角向左移动
            return;
        }
        
        if (Input.GetKeyDown(KeyCode.D))
        {
            // run();
            ChangePlayerZ(false); //移动主角向右移动
            return;
        }
        
        if (Input.GetKeyDown(KeyCode.F1))
        {
            //重新开始游戏
            SceneManager.LoadScene("Fight");
            return;
        }
#else
        if(BattleLogic.getInstance().GetIsPause())
            return;

        if (Input.touchCount <= 0)
            return;

        if (Input.GetTouch(0).phase == TouchPhase.Began && startPosFlag == true)
        {
            startFingerPos = Input.GetTouch(0).position;
            startPosFlag = false;
        }
        
        if (Input.GetTouch(0).phase == TouchPhase.Ended)
        {
            nowFingerPos = Input.GetTouch(0).position;
            if (nowFingerPos.x > startFingerPos.x)
            {
                ChangePlayerZ(false);
            }
            else if (nowFingerPos.x < startFingerPos.x)
            {
                ChangePlayerZ(true);
            }
            startPosFlag = true;
        }
#endif
    }

    /// <summary>
    /// 改变玩家左中右位置
    /// </summary>
    /// <param name="IsAD"></param>
    public void ChangePlayerZ(bool IsAD)
    {
        if (IsAD) //当按下键盘的A 也就是向左移动
        {
            //主角在最左边
            if (transform.parent.position.z == GameConfig.PLAYER_LEFT_Z)
                return;
            //主角在中间
            if (transform.parent.position.z == GameConfig.PLAYER_MID_Z)
            {
                transform.parent.position = new Vector3(transform.parent.position.x, transform.parent.position.y, GameConfig.PLAYER_LEFT_Z);
            }
            //主角在最右边
            else
            {
                transform.parent.position = new Vector3(transform.parent.position.x, transform.parent.position.y, GameConfig.PLAYER_MID_Z);
            }
        }
        else //当按下键盘的D 也就是向右移动
        {
            //主角在最右边
            if (transform.parent.position.z == GameConfig.PLAYER_RIGHT_Z)
                return;
            //主角在中间
            if (transform.parent.position.z == GameConfig.PLAYER_MID_Z)
            {
                transform.parent.position = new Vector3(transform.parent.position.x, transform.parent.position.y, GameConfig.PLAYER_RIGHT_Z);
            }
            //主角在最左边
            else
            {
                transform.parent.position = new Vector3(transform.parent.position.x, transform.parent.position.y, GameConfig.PLAYER_MID_Z);
            }
        }
    }

}